package com.example.stuwork.service.impl;

import com.example.stuwork.dao.DepartmentDao;
import com.example.stuwork.pojo.Department;
import com.example.stuwork.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 学院服务实现类
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public List<Department> findAll() {
        return departmentDao.findAll();
    }

    @Override
    public Department getByNumber(String deptNumber) {
        if (!StringUtils.hasText(deptNumber)) {
            return null;
        }
        return departmentDao.getByNumber(deptNumber);
    }

    @Override
    public boolean addDepartment(Department department) {
        if (department == null || !StringUtils.hasText(department.getDeptNumber()) 
                || !StringUtils.hasText(department.getDeptCode())
                || !StringUtils.hasText(department.getDeptName())) {
            return false;
        }
        
        // 检查编号、代码、名称是否已存在
        if (existsByNumber(department.getDeptNumber())) {
            return false;
        }
        if (existsByCode(department.getDeptCode(), null)) {
            return false;
        }
        if (existsByName(department.getDeptName(), null)) {
            return false;
        }
        
        return departmentDao.insert(department) > 0;
    }

    @Override
    public boolean updateDepartment(Department department) {
        if (department == null || !StringUtils.hasText(department.getDeptNumber())) {
            return false;
        }
        
        // 检查学院是否存在
        Department existingDept = getByNumber(department.getDeptNumber());
        if (existingDept == null) {
            return false;
        }
        
        // 检查代码和名称是否与其他学院冲突
        if (StringUtils.hasText(department.getDeptCode()) 
                && existsByCode(department.getDeptCode(), department.getDeptNumber())) {
            return false;
        }
        if (StringUtils.hasText(department.getDeptName()) 
                && existsByName(department.getDeptName(), department.getDeptNumber())) {
            return false;
        }
        
        return departmentDao.update(department) > 0;
    }

    @Override
    public boolean deleteDepartment(String deptNumber) {
        if (!StringUtils.hasText(deptNumber)) {
            return false;
        }
        
        // 检查学院是否存在
        if (!existsByNumber(deptNumber)) {
            return false;
        }
        
        // TODO: 检查学院是否有关联的专业和班级，有则不能删除
        
        return departmentDao.delete(deptNumber) > 0;
    }

    @Override
    public List<Department> search(String keyword) {
        if (!StringUtils.hasText(keyword)) {
            return findAll();
        }
        return departmentDao.search(keyword);
    }

    @Override
    public boolean existsByNumber(String deptNumber) {
        if (!StringUtils.hasText(deptNumber)) {
            return false;
        }
        return departmentDao.getByNumber(deptNumber) != null;
    }

    @Override
    public boolean existsByCode(String deptCode, String excludeNumber) {
        if (!StringUtils.hasText(deptCode)) {
            return false;
        }
        Department dept = departmentDao.getByCode(deptCode);
        if (dept == null) {
            return false;
        }
        // 如果是更新操作，排除自身
        if (StringUtils.hasText(excludeNumber) && dept.getDeptNumber().equals(excludeNumber)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean existsByName(String deptName, String excludeNumber) {
        if (!StringUtils.hasText(deptName)) {
            return false;
        }
        Department dept = departmentDao.getByName(deptName);
        if (dept == null) {
            return false;
        }
        // 如果是更新操作，排除自身
        if (StringUtils.hasText(excludeNumber) && dept.getDeptNumber().equals(excludeNumber)) {
            return false;
        }
        return true;
    }
} 